package fr.ginvest.implementation;

import java.util.Collection;
import java.util.Date;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import fr.ginvest.entity.Avancement;
import fr.ginvest.entity.Avancement.Etat;
import fr.ginvest.entity.Club;
import fr.ginvest.entity.Participation;
import fr.ginvest.entity.Stade;
import fr.ginvest.entity.Startup;
import fr.ginvest.interfaces.AvancementFacadeRemote;

@Stateless
public class AvancementFacade implements AvancementFacadeRemote {
	
	@PersistenceContext
	protected EntityManager entityMgr;
	public AvancementFacade() {
	}	

	public AvancementFacade(EntityManager entityMgr) {
		this.entityMgr = entityMgr;
	}

	
    public Avancement findAvancementById(int id) {
    	return entityMgr.find(Avancement.class, id);
    }

	/**
     * @see AvancementFacadeRemote#deleteAvancement(Avancement)
     */
    public void deleteAvancement(Avancement a) {
    	entityMgr.remove(a);
    }

	/**
     * @see AvancementFacadeRemote#updateAvancement(Avancement)
     */
    public Avancement updateAvancement(Avancement a) {
    	return entityMgr.merge(a);
    }

	/**
     * @see AvancementFacadeRemote#addAvancement(Avancement)
     */
    public Avancement addAvancement(Avancement a) {
    	entityMgr.persist(a);
		return a;
    }

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Stade> listStadeOfAvance(Avancement a) {
		String queryString = "select s from Avancement a inner join a.stades s where a=:avance order by s.id desc";
		Query query = entityMgr.createQuery(queryString).setParameter("avance", a);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Avancement> listAvancementByClub(Club club) {
		String queryString = "select a from Avancement a where a.club=:club";
		Query query = entityMgr.createQuery(queryString).setParameter("club", club);
		return query.getResultList();
	}

	@Override
	public Avancement toNextStade(Avancement a, Stade nextStade) {
		String queryString = "select a from Avancement a inner join a.stades s where a=:a";
		Query query = entityMgr.createQuery(queryString).setParameter("a", a);
		Avancement avance = (Avancement)query.getSingleResult();
		for(Participation p: a.getParticipations()){
			entityMgr.persist(p);
		}
		avance.setParticipations(a.getParticipations());
		entityMgr.persist(nextStade);
		avance.addStade(nextStade);
		return entityMgr.merge(avance);
	}

	@Override
	public Avancement closeAvancement(Avancement a) {
		a.setDateFin(new Date());
		a.setEtat(Etat.CLOTURE);
		return entityMgr.merge(a);
	}

	@Override
	public boolean hasOpenAvancement(Club c, Startup s) {
		String queryString = "select a from Avancement a where a.club=:c and a.startup=:s and a.etat=:etat";
		Query query = entityMgr.createQuery(queryString);
		query.setParameter("c", c);
		query.setParameter("s", s);
		query.setParameter("etat", Etat.EN_COURS);
		
		if(query.getResultList().size()>0)
			return true;
		
		return false;	
	}

	@Override
	public Collection<Participation> listParticipationOfAvance(Avancement a) {
		String queryString = "select p from Avancement a inner join a.participations p where a=:avance";
		Query query = entityMgr.createQuery(queryString).setParameter("avance", a);
		return query.getResultList();
	}

}
